﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SqlBlocks
{
    public class RawSqlMigration : Migration
    {
        public string UpFile { get; set; }
        public string DownFile { get; set; }

        public bool IsComplete
        {
            get
            {
                return File.Exists(UpFile) && File.Exists(DownFile);
            }
        }

        public override void Up(Changeset db)
        {
            DoSql(db, UpFile);
        }

        public override void Down(Changeset db)
        {
            DoSql(db, DownFile);
        }

        void DoSql(Changeset db, string fileName)
        {
            if (string.IsNullOrEmpty(fileName)) return;

            var file = new FileInfo(fileName);

            if (!file.Exists)
            {
                throw new FileNotFoundException("The specified migration file does not exist", file.FullName);
            }

            db.RunSql(File.ReadAllText(file.FullName));
        }
    }
}
